<template>
    <div class="box">
      <div class="box-sub">
        <el-form ref="form" :model="inputForm" label-width="80px">
          <el-form-item label="消息标题">
            <el-input v-model="inputForm.systemNewsTitle"></el-input>
          </el-form-item>
          <el-form-item label="定时发布">
            <el-date-picker
              v-model="inputForm.systemNewsTimingPublishDate"
              type="datetime"
              placeholder="选择日期时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="即时发布">
            <el-switch v-model="inputForm.systemNewsNowPublish"></el-switch>
          </el-form-item>
          <el-form-item label="消息类型">
            <el-radio-group v-model="inputForm.systemNewsType">
              <el-radio label="推广"></el-radio>
              <el-radio label="通知"></el-radio>
              <el-radio label="公告"></el-radio>
              <el-radio label="活动"></el-radio>
              <el-radio label="警告"></el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="消息内容" class="textarea">
            <el-input type="textarea" v-model="inputForm.systemNewsContent"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit" class="creat">立即创建</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
</template>

<script>
    export default {
        name: "AdminPublish",
        data() {
            return {
                inputForm:{
                    systemNewsId:'',
                    systemNewsTitle:'',
                    systemNewsTimingPublishDate:'',
                    systemNewsNowPublish:false,
                    systemNewsType:'',
                    systemNewsContent:''
                },
                baseURL:'http://localhost:8081/adminPage/publishNews/',
            }
        },
        mounted(){
            this.$emit("component","publish")
        },
        methods: {
            onSubmit() {
                if (this.inputForm.systemNewsTitle==""){
                    this.$message.warning("' 消息标题 ' 不能为空")
                } else if(this.inputForm.systemNewsTimingPublishDate=="" && this.inputForm.systemNewsNowPublish==false){
                    this.$message.warning("' 定时发布 ' 或 ' 即时发布 '，需二选一")
                }else if(this.inputForm.systemNewsType==""){
                    this.$message.warning("请选择 ' 消息类型 '")
                }else if(this.inputForm.systemNewsContent=="" || this.inputForm.systemNewsContent.length>255){
                    this.$message.warning("' 消息内容' 不能为空，且小于256个字")
                }
                else if(this.inputForm.systemNewsContent.indexOf(':') !=-1 || this.inputForm.systemNewsContent.indexOf('"') !=-1 ){
                    this.$message.warning("' 消息内容' 不能包含 \" \" 和 :")
                }
                else {
                    let parameters = JSON.stringify(this.inputForm)
                    this.axios.post(this.baseURL+parameters).then((res)=>{
                        if (res.data==true) {
                            this.successOpt("定时消息推送成功")
                        } else if(res.data=="trueForNow"){
                            this.successOpt("即时消息推送成功")
                        }else if (res.data==false){
                            this.failOpt("定时消息推送失败")
                        }else if(res.data=="falseForNow") {
                            this.failOpt("即时消息推送失败")
                        }else{
                            this.warningOpt("系统出现错误")
                        }
                    }).catch(
                        exception=>{
                            console.log(exception)
                        }
                    )
                }
            },

            successOpt(message) {
                this.$notify({
                    title: '成功',
                    message: message,
                    type: 'success'
                })
            },
            failOpt(message) {
                this.$notify.error({
                    title: '错误',
                    message: message
                });
            },
            warningOpt(message) {
                this.$notify.warning({
                    title: '提醒',
                    message: message
                });
            }
        },
    }
</script>

<style scoped>
    .creat{
      width: 100%;
    }
    .box{
      width: 100%;
      border-radius: 3px;
      background-color: white;
    }
    .box-sub{
      width: 80%;
      min-height: 800px;
      margin: 10px auto;
      padding-top: 30px;
      padding-bottom: 2px;
      background-color: white;
    }
</style>
